package com.yaorange.controller;

import com.github.pagehelper.PageInfo;
import com.yaorange.api.vo.Result;
import com.yaorange.entity.Article;
import com.yaorange.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


/**
 * @description:
 * @company: yaorange
 * @author: CorJ
 * @version: 1.0
 * @create: 2021-11-29 19:24
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @GetMapping("/page")
    public Result<?> pageList(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              String articleName,Integer articleType){
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("articleName",articleName);
        hashMap.put("articleType",articleType);
        PageInfo<Article> pageInfo = articleService.getPage(pageNum,pageSize,hashMap);
        if(pageInfo!=null){
            return Result.ok(pageInfo);
        }
        return Result.error("error");
    }
    @PostMapping
    public Result<?> add(@RequestParam String articleName,@RequestParam Integer stockNum,@RequestParam String stockUnit,@RequestParam Integer articleType,@RequestParam String articleDescribe){
        Article article = new Article();
        article.setArticleName(articleName);
        article.setStockNum(stockNum);
        article.setStockUnit(stockUnit);
        article.setArticleType(articleType);
        article.setArticleDescribe(articleDescribe);
        int i = articleService.addArticle(article);
        if(i>0){
            return Result.ok("新增成功");
        }
        return Result.error("新增失败");
    }
    @GetMapping("/getOne")
    public Result<?> getArticle(@RequestParam Integer id){
        Article article = articleService.getArticle(id);
        if(article!=null){
            return Result.ok(article);
        }
        return null;
    }
    @PutMapping
    public Result<?> update(@RequestBody Article article){
        int i = articleService.update(article);
        if(i>0){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id){
        if(articleService.delete(id)){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
    @DeleteMapping
    public Result<?> deleteMore(@RequestBody Integer[] ids){
        try{
                for (Integer id : ids) {
                    articleService.delete(id);
                }
            return Result.ok("删除成功");

        }catch (Exception e){
                return Result.error("删除失败");
            }
    }
@GetMapping("/{num}")
    public Result<?> checkNum(@PathVariable Integer num,@RequestParam Integer id){

            if(articleService.findNum(num,id)){
                return Result.ok();
            }

            return Result.error("err");
        }



}
